<?php
namespace Versal\SecurityBundle\Entity;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\Role\RoleInterface;
use Versal\SecurityBundle\Entity\RoleUser;
use Versal\SecurityBundle\Entity\Usuario;
use Versal\SecurityBundle\Entity\Password;
use Versal\SecurityBundle\Entity\Perfil;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of User
 *
 * @author Jorge
 */
class User implements UserInterface
{
    private $usuario;
    private $password;
    private $perfiles;
    
    
 
    public function __construct(Usuario $usuario, Password $password , array $perfiles)
    {
        $this->usuario = $usuario;
        $this->password = $password;
        $this->perfiles = $perfiles;
        
    }
     
    /*
     * @override
     * 
     */
    public function supportsClass($class)
    {
        return $this->getEntityName() === $class || is_subclass_of($class, $this->getEntityName());
    }
    
	/*
     * @override
     * 
     */
    public function eraseCredentials() 
    {
        
    }
	
    /*
     * @override
     * 
     */
    public function getPassword() 
    {
        return $this->password->getPassword();
    }

    /*
     * @override
     * 
     */
    public function getRoles() 
    {
        $arrayRole = array();
        foreach($this->perfiles as $perfil){
            $rol = new RoleUser($perfil->getPerfil());
            $rol->setIdApp($perfil->getIdApp());
            $arrayRole[] = $rol;
        }
        return $arrayRole;
    }
        
	/*
     * @override
     * 
     */
    public function getSalt() 
    {
        
    }
    
	/*
     * @override
     * 
     */
    public function getUsername() 
    {
        return $this->usuario->getUsername();
    }
    
    
    public function getPerfilByApp($appId) 
    {
        
        foreach($this->perfiles as $perfil){
            if($perfil->getIdApp() == $appId){
               return $perfil; 
            }
        }
            
        return null;
    }
    
    public function getUsuario()
    {
        return $this->usuario;
    }
    
    public function getPerfiles()
    {
        return $this->perfiles;
        
    }
    /*
     public function serialize()
    {
        
        return \serialize(array(
           // $this->id,
            $this->usuario->getUsername(),
          //  $this->email,
           // $this->salt,
            $this->password->getPassword()
           //$this->perfiles
        ));
    }
*/
    /*
    public function unserialize($serialized)
    {
        /*$username = $this->usuario->getUsername();
        $password = $this->password->getPassword();
        list (
           // $this->id,
            $username,
           // $this->email,
           // $this->salt,
            $password
           // $this->perfiles
        ) = \unserialize($serialized);
    }*/
       
}

